from datetime import datetime

from sqlmodel import SQLModel, Field, Relationship

from models.relationships.role_permission import RolePermissionLink
from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from models.role import Role


class Permission(SQLModel, table=True):
    id: int = Field(default=None, primary_key=True)
    name: str = Field(max_length=30, unique=True, index=True)
    description: str = Field(max_length=100)
    created_at: datetime = Field(default_factory=datetime.now)
    updated_at: datetime = Field(default_factory=datetime.now)
    father_id: int = Field(default=0)
    roles: list["Role"] = Relationship(back_populates="permissions", link_model=RolePermissionLink)
